package com.yiqi.zhiyuan.feature.login.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.suozhang.framework.entity.bo.UserInfoBo;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.TextUtil;
import com.suozhang.framework.widget.PowerfulEditText;
import com.yiqi.zhiyuan.R;
import com.yiqi.zhiyuan.feature.certification.view.UserInformationActivity;
import com.yiqi.zhiyuan.feature.login.contract.RegisterContract;
import com.yiqi.zhiyuan.feature.login.dependencies.register.DaggerRegisterComponent;
import com.yiqi.zhiyuan.feature.login.dependencies.register.RegisterPresenterModule;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

/**
 * @ProjectName: ZhiYuan
 * @Package: com.yiqi.zhiyuan.feature.login.view
 * @ClassName: RegisterActivity
 * @Description: java类作用描述
 * @Author:
 * @CreateDate: 2019/3/11 17:34
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/3/11 17:34
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class RegisterActivity extends BaseActivity implements PowerfulEditText.TextListener, RegisterContract.View, CompoundButton.OnCheckedChangeListener {
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.edt_register_phone)
    PowerfulEditText mEdtRegisterPhone;
    @BindView(R.id.iv_register_code)
    ImageView mIvRegisterCode;
    @BindView(R.id.tv_register_post_code)
    TextView mTvRegisterPostCode;
    @BindView(R.id.rl_register_code)
    RelativeLayout mRlRegisterCode;
    @BindView(R.id.edt_register_code)
    PowerfulEditText mEdtRegisterCode;
    @BindView(R.id.iv_register_pwd)
    ImageView mIvRegisterPwd;
    @BindView(R.id.edt_register_pwd)
    PowerfulEditText mEdtRegisterPwd;
    @BindView(R.id.btn_register_commit)
    TextView mBtnRegister;
    @BindView(R.id.tv_agree_agreement)
    TextView mTvAgreeAgreement;
    @BindView(R.id.cbc_register_service)
    CheckBox mCbcRegisterService;

    @Inject
    RegisterContract.Presenter mPresenter;


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_register;
    }

    @Override
    protected void initView() {
        initToolBar(mToolbar, getString(R.string.register_title), true, true);
        mTvAgreeAgreement.setText(TextUtil.setTextViewColor(getString(R.string.register_agree_agreement_click) + getString(R.string.agree_agreement), getString(R.string.agree_agreement), getResources().getColor(R.color.login_reset_pwd)));

    }

    @Override
    protected void initInjector() {
        DaggerRegisterComponent.builder().registerPresenterModule(new RegisterPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {
        mEdtRegisterPhone.addTextListener(this);
        mEdtRegisterCode.addTextListener(this);
        mEdtRegisterPwd.addTextListener(this);
        mCbcRegisterService.setOnCheckedChangeListener(this);
    }


    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, 0);
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (!TextUtils.isEmpty(mEdtRegisterPhone.getText().toString()) &&
                !TextUtils.isEmpty(mEdtRegisterCode.getText().toString()) &&
                !TextUtils.isEmpty(mEdtRegisterPwd.getText().toString()) &&
                mCbcRegisterService.isChecked()) {
            mBtnRegister.setEnabled(true);
        } else {
            mBtnRegister.setEnabled(false);
        }
    }

    @Override
    public void showGetCodeSuccess() {
        showMsg(R.string.toast_code_ok);
        onGetCode();
    }

    @Override
    public void showGetCodeError(String errorMsg) {
        showMsg(errorMsg);
    }

    @Override
    public void showRegisterSuccess(UserInfoBo userInfoBo) {
        Intent intent = new Intent(this, UserInformationActivity.class);
        intent.putExtra("is_first", true);
        startActivity(intent);
        finish();
        overridePendingTransition(0, 0);
    }

    @Override
    public void showRegisterError(String errorMsg) {
        showMsg(errorMsg);
    }

    @OnClick(R.id.tv_register_post_code)
    public void onViewCodeClicked() {
        mPresenter.sendCode(mEdtRegisterPhone.getText().toString());
    }


    @OnClick(R.id.btn_register_commit)
    public void onViewCommitClicked() {
        mPresenter.register(mEdtRegisterPhone.getText().toString(), mEdtRegisterPwd.getText().toString(), mEdtRegisterCode.getText().toString());
    }

    public void onGetCode() {
        final int count = 60;//倒计时60秒
        Observable.interval(0, 1, TimeUnit.SECONDS)
                .take(count + 1)
                .map(new Function<Long, Long>() {
                    @Override
                    public Long apply(Long aLong) throws Exception {
                        return count - aLong;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())//ui线程中进行控件更新
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        mTvRegisterPostCode.setEnabled(false);
                        mTvRegisterPostCode.setTextColor(AM.res().getColor(R.color.text_secondary));
                    }
                }).subscribe(new Observer<Long>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Long num) {
                mTvRegisterPostCode.setText(num + "s");
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {
                //回复原来初始状态
                mTvRegisterPostCode.setEnabled(true);
                mTvRegisterPostCode.setText("获取验证码");
                mTvRegisterPostCode.setTextColor(AM.res().getColor(R.color.primary_highlight));
            }
        });

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (!TextUtils.isEmpty(mEdtRegisterPhone.getText().toString()) &&
                !TextUtils.isEmpty(mEdtRegisterCode.getText().toString()) &&
                !TextUtils.isEmpty(mEdtRegisterPwd.getText().toString()) &&
                isChecked) {
            mBtnRegister.setEnabled(true);
        } else {
            mBtnRegister.setEnabled(false);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.tv_agree_agreement)
    public void onViewClicked() {
    startActivity(new Intent(this,RegistrationAgreementActivity.class));
    }
}
